######################### AGGREGATE REGRESSION ANALYSIS ############################
####################################################################################

#Name of code file: regression_analysis.R
#Purpose: Regression Models for Aggregate Data
#Data In: network_tolerance_processed_data.csv
#Data Out: Figures: 4 Tables: B1, B2, B4, B5-B7 
####################################################################################

#Load packages
library(readr)
library(coefplot)
library(apsrtable)
library(MASS)
library(AER)
library(ggplot2)
library(estout)
library(interplot)
library(xtable)

#Set Working Directory
setwd("~/Dropbox/egypt_tolerance_wp_replication/")

#Read in Data
data<-read_csv("data/network_tolerance_processed_data.csv")
thresholds<-read_csv("data/thresholds.csv")

##########
#TABLE B1#
##########

#Quasi Poisson Intolerant Count Model 
quasipoisson_intolerant_model <- glm(data$intolerant_total ~ data$elite_diversity + data$nonelite_diversity +data$log_total_friends +data$log_elite+ data$log_relevant_tweets + data$log_time +  data$islamist, family=quasipoisson())
summary(quasipoisson_intolerant_model) 
exp_quasi<-as.data.frame(exp(coef(quasipoisson_intolerant_model)))
xtable(exp_quasi)

#Run Without Controls
quasipoisson_intolerant_model_no_cont <- glm(data$intolerant_total ~ data$elite_diversity + data$nonelite_diversity , family=quasipoisson())
summary(quasipoisson_intolerant_model_no_cont) 

#Proportion Intolerant OLS Model 
prop_intolerant_model <- lm(data$prop_intolerant ~ data$elite_diversity + data$nonelite_diversity +data$log_total_friends +data$log_elite+ data$log_relevant_tweets+ data$log_time + data$islamist)
summary(prop_intolerant_model) 

#Run Without Controls
prop_intolerant_model_no_cont <- lm(data$prop_intolerant ~ data$elite_diversity + data$nonelite_diversity)
summary(prop_intolerant_model_no_cont) 

#Make Table
apsrtable(quasipoisson_intolerant_model_no_cont, quasipoisson_intolerant_model, prop_intolerant_model_no_cont, prop_intolerant_model, digits=4, lev=.1, stars="default")

##########
#TABLE B2#
##########

#Same as Table B1, but including moderates

quasipoisson_intolerant_model_moderate <- glm(data$intolerant_total ~ data$elite_diversity+ data$nonelite_diversity_mod+data$log_total_friends +data$log_elite+ data$log_relevant_tweets + data$log_time +  data$islamist, family=quasipoisson())
summary(quasipoisson_intolerant_model_moderate) 
exp_quasi<-as.data.frame(exp(coef(quasipoisson_intolerant_model_moderate)))
xtable(exp_quasi)

quasipoisson_intolerant_model_no_cont_moderate <- glm(data$intolerant_total ~ data$elite_diversity+ data$nonelite_diversity_mod, family=quasipoisson())
summary(quasipoisson_intolerant_model_no_cont_moderate) 

prop_intolerant_model_moderate <- lm(data$prop_intolerant ~ data$elite_diversity+ data$nonelite_diversity_mod+data$log_total_friends +data$log_elite+ data$log_relevant_tweets + data$log_time +  data$islamist)
summary(prop_intolerant_model_moderate) 

prop_intolerant_model_no_cont_moderate <- lm(data$prop_intolerant ~ data$elite_diversity+ data$nonelite_diversity_mod)
summary(prop_intolerant_model_no_cont_moderate) 

#Make table
apsrtable(quasipoisson_intolerant_model_no_cont_moderate, quasipoisson_intolerant_model_moderate, quasipoisson_intolerant_model_no_cont_moderate, prop_intolerant_model_moderate, digits=4, lev=.1, stars="default")


##########
#Figure 4#
##########

new_names<-c("data$elite_diversity" = "Elite Diversity", "data$nonelite_diversity_mod" = "Non-Elite Diversity \n (w/ Moderates)", "data$nonelite_diversity"="Non-Elite Diversity\n (w/o Moderates)", "data$log_total_friends" = "Log Non-Elite Friends", "data$log_elite" ="Log Elite Friends", "data$log_relevant_tweets"="Log Relevant Tweets", "data$log_time"=" Log Days on Twitter", "data$log_status_count"="Log Total Tweets", "data$secular"="Secular", "data$islamist"="Islamist", "data$moderate"="Moderate")
pdf(file="plots/figure4.pdf", width = 11, height = 7)
plot<-multiplot(quasipoisson_intolerant_model, quasipoisson_intolerant_model_moderate, innerCI=1.645, outerCI=1.96,  sort="alphabetical",single=FALSE, coefficients=c("data$elite_diversity","data$nonelite_diversity_mod", "data$nonelite_diversity"), plot=TRUE,intercept=FALSE, ylab="", xlab="", newNames=new_names, decreasing=FALSE, pointSize=5, title="", names=c("Without Moderates", "With Moderates"),legend.position="bottom", zeroColor="black") 
plot+ theme_bw(base_size=22)+ theme(legend.position="none")+scale_color_manual(values=c("black", "black"))
dev.off()

##########
#TABLE B4#
##########

#Quasi Poisson Tolerant Count Model 
quasipoisson_tolerant_model <- glm(data$tolerant_total ~ data$elite_diversity + data$nonelite_diversity +data$log_total_friends +data$log_elite+ data$log_relevant_tweets + data$log_time +  data$islamist, family=quasipoisson())
summary(quasipoisson_tolerant_model) 
exp_quasi<-as.data.frame(exp(coef(quasipoisson_tolerant_model)))
xtable(exp_quasi)

#Re-Run Without Controls
quasipoisson_tolerant_model_no_cont <- glm(data$tolerant_total ~ data$elite_diversity + data$nonelite_diversity, family=quasipoisson())
summary(quasipoisson_tolerant_model_no_cont) 


#Proportion Tolerant OLS Model 
prop_tolerant_model <- lm(data$prop_tolerant ~ data$elite_diversity + data$nonelite_diversity +data$log_total_friends +data$log_elite+ data$log_time + data$islamist)
summary(prop_tolerant_model) 

#Re-Run Without Controls
prop_tolerant_model_no_cont <- lm(data$prop_tolerant ~ data$elite_diversity + data$nonelite_diversity)
summary(prop_tolerant_model) 

#Make Table
apsrtable(quasipoisson_tolerant_model_no_cont, quasipoisson_tolerant_model, prop_tolerant_model_no_cont, prop_tolerant_model, digits=4, lev=.1, stars="default")

##########
#TABLE B5#
##########

data$irrelevant_total<-data$total-data$relevant_total

#Quasi Poisson Irrelevant Count Model 

quasipoisson_irrelevant_model <- glm(data$irrelevant_total ~ data$elite_diversity + data$nonelite_diversity +data$log_total_friends +data$log_elite+ data$log_time + data$log_relevant_tweets+ data$islamist, family=quasipoisson())
summary(quasipoisson_irrelevant_model) 
exp_quasi<-as.data.frame(exp(coef(quasipoisson_tolerant_model)))
xtable(exp_quasi)

#Re-Run without Controls
quasipoisson_irrelevant_model_no_cont <- glm(data$irrelevant_total ~ data$elite_diversity + data$nonelite_diversity, family=quasipoisson())
summary(quasipoisson_irrelevant_model_no_cont) 

#Proportion Irrelevant OLS Model 
data$prop_irrelevant<-data$irrelevant_total/data$total
prop_irrelevant_model <- lm(data$prop_irrelevant~ data$elite_diversity + data$nonelite_diversity +data$log_total_friends +data$log_elite+ data$log_time + data$islamist)
summary(prop_irrelevant_model) 

#Re-Run without controls
prop_irrelevant_model_no_cont <- lm(data$prop_irrelevant~ data$elite_diversity + data$nonelite_diversity)
summary(prop_irrelevant_model_no_cont)

#Make Table
apsrtable(quasipoisson_irrelevant_model_no_cont, quasipoisson_irrelevant_model, prop_irrelevant_model_no_cont, prop_irrelevant_model, digits=4, lev=.1, stars="default")


##########
#TABLE B6#
##########

#Negative Binomial Model
negbinom_intolerant_model <- glm.nb(data$intolerant_total ~ data$elite_diversity + data$nonelite_diversity +data$log_total_friends +data$log_elite+ data$log_relevant_tweets + data$log_time +  data$islamist, init.theta = 1, link = log)
summary(negbinom_intolerant_model) 
apsrtable(negbinom_intolerant_model, digits=4, lev=.1, stars="default")

##########
#TABLE B7#
##########

#Replicate quasipoisson analysis for each thershold of non-elite network diversity 

data$id2<-data$id
data<-merge(data, thresholds, by="id2", all=TRUE)

model55<- glm(data$intolerant_total ~ data$elite_diversity55 + data$nonelite_diversity55 +data$log_total_friends +data$log_elite+ data$log_relevant_tweets + data$log_time + data$islamist55, family=quasipoisson())
summary(model55) 

model65<- glm(data$intolerant_total ~ data$elite_diversity65 + data$nonelite_diversity65 +data$log_total_friends +data$log_elite+ data$log_relevant_tweets + data$log_time + data$islamist65, family=quasipoisson())
summary(model65) 

model70<- glm(data$intolerant_total ~ data$elite_diversity70 + data$nonelite_diversity70 +data$log_total_friends +data$log_elite+ data$log_relevant_tweets + data$log_time + data$islamist70, family=quasipoisson())
summary(model70) 

model75<- glm(data$intolerant_total ~ data$elite_diversity75 + data$nonelite_diversity75 +data$log_total_friends +data$log_elite+ data$log_relevant_tweets + data$log_time + data$islamist75, family=quasipoisson())
summary(model75) 

model80<- glm(data$intolerant_total ~ data$elite_diversity80 + data$nonelite_diversity80 +data$log_total_friends +data$log_elite+ data$log_relevant_tweets + data$log_time + data$islamist80, family=quasipoisson())
summary(model80) 

model85<- glm(data$intolerant_total ~ data$elite_diversity85 + data$nonelite_diversity85 +data$log_total_friends +data$log_elite+ data$log_relevant_tweets + data$log_time + data$islamist85, family=quasipoisson())
summary(model85) 

#Make Table
apsrtable(model55, quasipoisson_intolerant_model, model65, model70, model75, model80, model85, digits=4, lev=.1, stars="default")




